package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsControlRecordDetail;
import com.econ.powercloud.ems.dal.mapper.EmsControlRecordDetailMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author jwc
 * @since 2023/06/01
 */
@Repository
public class EmsControlRecordDetailDAO extends ServiceImpl<EmsControlRecordDetailMapper, EmsControlRecordDetail> {
    private static final String LIMIT_ONE = "LIMIT 1";

    public EmsControlRecordDetail selectByMsgId(Integer msgId) {
        LambdaQueryWrapper<EmsControlRecordDetail> wrapper = Wrappers.<EmsControlRecordDetail>lambdaQuery()
                .eq(EmsControlRecordDetail::getMsgId, msgId);
        return this.getBaseMapper().selectOne(wrapper);
    }

    public EmsControlRecordDetail selectByRecordIdOrderByStepId(Long recordId) {
        LambdaQueryWrapper<EmsControlRecordDetail> wrapper = Wrappers.<EmsControlRecordDetail>lambdaQuery()
                .eq(EmsControlRecordDetail::getRecordId, recordId)
                .orderByAsc(EmsControlRecordDetail::getStepNum)
                .last(LIMIT_ONE);
        return this.getBaseMapper().selectOne(wrapper);
    }

    public EmsControlRecordDetail selectByRecordIdAndStepId(Long recordId, Integer stepId) {
        LambdaQueryWrapper<EmsControlRecordDetail> wrapper = Wrappers.<EmsControlRecordDetail>lambdaQuery()
                .eq(EmsControlRecordDetail::getRecordId, recordId)
                .gt(EmsControlRecordDetail::getStepNum, stepId)
                .orderByAsc(EmsControlRecordDetail::getStepNum)
                .last(LIMIT_ONE);
        return this.getBaseMapper().selectOne(wrapper);
    }

    public List<EmsControlRecordDetail> selectByStatusAndMsgIdIsNotNull(Integer status) {
        LambdaQueryWrapper<EmsControlRecordDetail> wrapper = Wrappers.<EmsControlRecordDetail>lambdaQuery()
                .eq(EmsControlRecordDetail::getStatus, status)
                .isNotNull(EmsControlRecordDetail::getMsgId);
        return this.getBaseMapper().selectList(wrapper);
    }
}
